home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / JunkMatcher 1.5.5.dmg / JunkMatcher.app / Contents / Resources / Engine / utilities.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2005-06-01  |  3.9 KB  |  88 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. import sets
  5. from consts import *
  6. import codecs
  7. import cjkcodecs
  8. from cjkcodecs import aliases
  9. import objc
  10. from Foundation import *
  11. charsetMispellings = {
  12.     'big-5': 'big5' }
  13. _DECODE_ERROR_STRATEGY = 'ignore'
  14. _ENCODE_ERROR_STRATEGY = 'ignore'
  15. _DEFAULT_ENCODING = 'utf8'
  16.  
  17. class Lazy(object):
  18.     '''A discriptor class used for decorator: for lazy initialization.'''
  19.     
  20.     def __init__(self, calculate_function):
  21.         self._calculate = calculate_function
  22.  
  23.     
  24.     def __get__(self, obj, _ = None):
  25.         value = self._calculate(obj)
  26.         setattr(obj, self._calculate.func_name, value)
  27.         return value
  28.  
  29.  
  30.  
  31. def decodeText(text, encoding = None):
  32.     """Decode a text encoded by 'encoding' - if 'encoding' is None, decode using UTF-8
  33.     encoding; returns a Unicode object."""
  34.     if encoding:
  35.         
  36.         try:
  37.             return text.decode(encoding, _DECODE_ERROR_STRATEGY)
  38.         return text.decode(_DEFAULT_ENCODING, _DECODE_ERROR_STRATEGY)
  39.  
  40.     
  41.     return text.decode(_DEFAULT_ENCODING, _DECODE_ERROR_STRATEGY)
  42.  
  43.  
  44. def decodeTextList(textList, encodingSet):
  45.     '''Decode textList: textList is a list of tuples (text, encoding); encodings will be
  46.     added to encodingSet; returns a Unicode object.'''
  47.     strList = []
  48.     for t, e in textList:
  49.         mispelling = charsetMispellings.get(e)
  50.         if mispelling:
  51.             e = mispelling
  52.         
  53.         if e:
  54.             encodingSet.add(e)
  55.         
  56.         strList.append(decodeText(t, e))
  57.     
  58.     return ''.join(strList)
  59.  
  60.  
  61. def encodeText(text, encoding = _DEFAULT_ENCODING):
  62.     '''Encode text using the given encoding; if encoding is None use
  63.     the _DEFAULT_ENCODING.'''
  64.     if encoding:
  65.         
  66.         try:
  67.             return text.encode(encoding, _ENCODE_ERROR_STRATEGY)
  68.         return text.encode(_DEFAULT_ENCODING, _ENCODE_ERROR_STRATEGY)
  69.  
  70.     
  71.     return text.encode(_DEFAULT_ENCODING, _ENCODE_ERROR_STRATEGY)
  72.  
  73.  
  74. def openFile(fn, mode = 'r', encoding = _DEFAULT_ENCODING):
  75.     '''Open a file using an encoding - every user editable file (via GUI) should
  76.     be opened using this with a proper encoding (default: utf8)!'''
  77.     return codecs.open(fn, mode, encoding, 'strict')
  78.  
  79.  
  80. def printException(msg, e):
  81.     '''Use NSLog to print information about Exception e.'''
  82.     info = str(e)
  83.     if info:
  84.         NSLog(u'%s: %s' % (msg, info))
  85.     else:
  86.         NSLog(u'%s' % msg)
  87.  
  88.